package com.mygdx.game.desktop.demo;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;

/**
 * .碰撞检测
 * @author Administrator
 *
 */
public class CollisionTestGame extends Game {

    ShapeRenderer rend;
    Polygon polygon1, polygon2;
    float[] vertices1, vertices2;
    Vector2 point = new Vector2(100, 50);
    
    @Override
    public void create() {
        
        // 多边形1
        polygon1 = new Polygon();
        vertices1 = new float[]{ 100, 100, 200, 100, 300, 300, 100, 200 };
        polygon1.setVertices(vertices1);
        // 多边形2   
        vertices2 = new float[]{ 100, 50, 200, 70, 300, 150, 150, 100};
        polygon2 = new Polygon(vertices2);
        
        rend = new ShapeRenderer();
        
        Gdx.input.setInputProcessor(adapter);
    }

    public void render() {
    	super.render();
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        rend.begin(ShapeType.Line);
        rend.setColor(Color.RED);
        for(int i=0; i<polygon1.getVertices().length; i+=2){
        	rend.line(vertices1[i], vertices1[i+1], vertices1[(i+2)%vertices1.length], vertices1[(i+3)%vertices1.length]);
        }
             
        float[] vertices3 = polygon2.getVertices();
	    for(int i=0; i<polygon2.getVertices().length; i+=2){
	        rend.line(vertices3[i], vertices3[i+1], vertices3[(i+2)%vertices3.length], vertices3[(i+3)%vertices3.length]);
        }
	    rend.end();
	    
	    if( isOverlap(polygon1, polygon2) ){
	    	rend.setColor(Color.RED);
	    }else{
	    	rend.setColor(Color.BLUE);
	    }
	    rend.begin(ShapeType.Filled);
	    rend.circle(point.x, point.y, 5);
	    rend.end();
    }

    public void dispose() {

    }

    InputAdapter adapter = new InputAdapter(){
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {            
	    for(int i=0; i<polygon2.getVertices().length; i+=2){
	    polygon2.getVertices()[i  ] += screenX - point.x;
	    polygon2.getVertices()[i+1] += Gdx.graphics.getHeight()-screenY - point.y;
	    }
	    polygon2.dirty();
	    point.set(screenX, Gdx.graphics.getHeight()-screenY);
	    //polygon2.setVertices(new float[]{ 100+point.x, 50+point.y, 200+point.x, 70+point.y, 300+point.x, 150+point.y, 150+point.x, 100+point.y});
		return true;
		}
    };
    
    /**
     * 是否碰撞
     * @param polygon1
     * @param polygon2
     * @return
     */
    public static boolean isOverlap(Polygon polygon1, Polygon polygon2){
	    for(int i=0; i<polygon2.getVertices().length; i+=2){
		    if( polygon1.contains(polygon2.getVertices()[i], polygon2.getVertices()[i+1]) ){
		    	return true;
		    }
	    }
	    for(int i=0; i<polygon1.getVertices().length; i+=2){
		    if( polygon2.contains(polygon1.getVertices()[i], polygon1.getVertices()[i+1]) ){
		    	return true;
		    }
	    }
	    return false;
    }
    
    public static void main(String[] args) {
        new LwjglApplication(new CollisionTestGame());
    }
}
